package com.hongning.railway.ui.categorylistnews;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hongning.railway.R;
import com.hongning.railway.app.AppViewModelFactory;
import com.hongning.railway.databinding.FragmentCategorylistnewsBinding;
import com.hongning.railway.entity.resbean.NewsCategoryBean;
import com.hongning.railway.ui.main.fragments.home.HomeViewModel;
import com.hongning.railway.util.Apputils;

import me.goldze.mvvmhabit.BR;
import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.tatarka.bindingcollectionadapter2.BindingRecyclerViewAdapter;

public class CategoryListNewsFragment extends BaseFragment<FragmentCategorylistnewsBinding,CategoryListNewsFragmentViewModel> {
    public static String PARAMS_CATEGORY_ID="params_category_id";
    private NewsCategoryBean newsCategoryBean;


    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_categorylistnews;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }
    @Override
    public CategoryListNewsFragmentViewModel initViewModel() {
        AppViewModelFactory appViewModelFactory= AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, appViewModelFactory).get(CategoryListNewsFragmentViewModel.class);
    }

    @Override
    public void initParam() {
        super.initParam();
        if (getArguments()!=null){
            newsCategoryBean =getArguments().getParcelable(PARAMS_CATEGORY_ID);
        }

    }

    @Override
    public void initData() {
        super.initData();
        if (newsCategoryBean!=null){
            binding.topTitle.tvTopTitle.setText(newsCategoryBean.getName());
            binding.topTitle.ivLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getActivity().finish();
                }
            });
            Apputils.setDefaultHeaderView(binding.trRefhreh);
            binding.rvNews.setAdapter(new BindingRecyclerViewAdapter());
            if (!TextUtils.isEmpty(String.valueOf(newsCategoryBean.getId())))
                viewModel.setCateId(String.valueOf(newsCategoryBean.getId()));
            viewModel.requestNewsList(String.valueOf(newsCategoryBean.getId()));
        }else {
            binding.topTitle.tvTopTitle.setText("最新资讯");
            binding.topTitle.ivLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getActivity().finish();
                }
            });
            Apputils.setDefaultHeaderView(binding.trRefhreh);
            binding.rvNews.setAdapter(new BindingRecyclerViewAdapter());
            viewModel.requestNewsListbytime();
        }

    }


    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.finishRefreshing.observe(this, new Observer() {
            @Override
            public void onChanged(@Nullable Object o) {
                binding.trRefhreh.finishRefreshing();
                binding.trRefhreh.setEnableLoadmore(true);
//                binding.trRefhreh.setHeaderView();
            }
        });

        viewModel.uc.finishLoadmore.observe(this, new Observer() {
            @Override
            public void onChanged(@Nullable Object o) {
                binding.trRefhreh.finishLoadmore();
            }
        });

        viewModel.uc.enableLoadmore.observe(this, new Observer() {
            @Override
            public void onChanged(@Nullable Object o) {
                binding.trRefhreh.setEnableLoadmore(false);
            }
        });
        viewModel.uc.emptyList.observe(this, new Observer() {
            @Override
            public void onChanged(@Nullable Object o) {
                binding.includeAppstatus.rlContent.setVisibility(View.VISIBLE);
                binding.includeAppstatus.tvStatu.setText("暂无内容");
                binding.includeAppstatus.ivStatu.setImageResource(R.mipmap.icon_nocontent);
            }
        });
    }
}
